from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.template import RequestContext
from company.models import *
from documents.models import *
from Person.models import *
from django.contrib.auth.decorators import login_required


@login_required
def index(request):
    companies =  Company.objects.all()
    return render_to_response('Company/company.html', {'companies': companies})
    
@login_required
def viewEmployee(request, eid, pkid):
    e = Employee.objects.get(id_no = eid)
    return render_to_response('Company/editEmployee.html', {'e': e})
    
 
@login_required
def details(request, pkid):
    c =  Company.objects.get(pk = pkid)
    docs = c.docs.all()
    employees = Employee.objects.filter(company = c)
    return render_to_response('Company/details.html', {'company': c, 'docs': docs, 'emps': employees})
    
    
@login_required
def addDoc(request, pkid):
    company =  Company.objects.get(pk = pkid)
    form =  documentForm()
    c = {}
    c.update(csrf(request))
    return render_to_response('Company/addDoc.html', {'doc': form, 'org': company},context_instance=RequestContext(request) )

@login_required
def addDocEmployee(request, pkid, eid):
    type = 'Document'
    if request.method == 'POST': # If the form has been submitted...
         e = Employee.objects.get(id_no = eid)
         company =  Company.objects.get(pk = pkid)
         doc = Document()
         form = documentForm(request.POST, request.FILES, instance=doc)
         if form.is_valid():
              if doc.arabic_org_day is not None:
                   doc.arabic_to_gorg()
              e.docs.add(doc)
              e.save()              
              doc.save()

          


              
              return render_to_response('Company/editEmployee.html', {'org': company, 'e': e},context_instance=RequestContext(request) )

    else:  
         e = Employee.objects.get(id_no = eid)
         company =  Company.objects.get(pk = pkid)
         form =  documentForm()

         c = {}
         c.update(csrf(request))
         return render_to_response('Company/addEmpDoc.html', {'doc': form, 'org': company, 'emp': e, 'type': type},context_instance=RequestContext(request) )

@login_required
def viewDoc(request, pkid, docid):
    if request.method == 'POST': # If the form has been submitted...
        company = Company.objects.get(pk = pkid)
        doc = company.docs.get(pk = docid)
        form = documentForm(request.POST, request.FILES, instance=doc)
        if form.is_valid():
            if doc.arabic_org_day is not None:
                if doc.arabic_exp_day is not None:
                     doc.arabic_to_gorg()
            doc = form.save()
            #doc.save()
            #company.docs.add(doc)
            company.save()
            return HttpResponseRedirect('../') # Redirect after POST
        else:
            form =  documentForm(request.POST)

            c = {}
            c.update(csrf(request))
            return render_to_response('Company/editDoc.html', {'doc': form, 'org': company},context_instance=RequestContext(request) )
    else:
	    company = Company.objects.get(pk = pkid)
	    doc = company.docs.get(pk = docid)
 	    form =  documentForm(instance=doc)
	    return render_to_response('Company/editDoc.html', {'doc': form, 'org': company},context_instance=RequestContext(request) )

@login_required
def validateDoc(request, pkid):
    if request.method == 'POST': # If the form has been submitted...
        doc = Document()
        company =  Company.objects.get(pk = pkid)
        form = documentForm(request.POST, request.FILES, instance=doc)
        if form.is_valid():
            if doc.arabic_org_day is not None:
                doc.arabic_to_gorg()

            doc.save()
            company.docs.add(doc)
            company.save()
            return HttpResponseRedirect('../') # Redirect after POST
        else:
            form =  documentForm(request.POST)

            c = {}
            c.update(csrf(request))
            return render_to_response('Company/addDoc.html', {'doc': form, 'org': company},context_instance=RequestContext(request) )
@login_required
def validateEditDoc(request, pkid, docid):
    if request.method == 'POST': # If the form has been submitted...
        company = Company.objects.get(pk = pkid)
        doc = company.docs.get(pk = docid)
        form = documentForm(request.POST, request.FILES, instance=doc)
        if form.is_valid():
            if doc.arabic_org_day is not None:
                doc.arabic_to_gorg()
            doc = form.save()
            #doc.save()
            #company.docs.add(doc)
            company.save()
            return HttpResponseRedirect('http://www.yahoo.com') # Redirect after POST
        else:
            form =  documentForm(request.POST)

            c = {}
            c.update(csrf(request))
            return render_to_response('Company/editDoc.html', {'doc': form, 'org': company},context_instance=RequestContext(request) )
